/**
 * Created by midori on 2015/12/8.
 */
(function(){
    var PromiseWrapper, module;

    module = angular.module("services.Utils", []);

    module.factory('utils', [
        function() {
            var utilsInstance;
            utilsInstance = {};
            utilsInstance.defaultMaxStringLength = 256;
            utilsInstance.stringStartsWith = function(str, supposedPrefix) {
                return str.slice(0, supposedPrefix.length) === supposedPrefix;
            };
            utilsInstance.stringContains = function(str, otherString) {
                var integer;
                integer = str.indexOf(otherString);
                return integer >= 0;

            };
            utilsInstance.stringContainsIgnoreCase = function(str, otherString) {
                var integer;
                integer = str.toLowerCase().indexOf(otherString.toLowerCase());
                return integer >= 0;

            };
            utilsInstance.getFeatureForActivePlan = function(product, featureId, featureService, customerService) {
                var bookingHandler;
                bookingHandler = function(booking) {
                    if (!((booking != null) && (booking.planId != null))) {
                        console.warn("'getFeatureForActivePlan' was called with an undefined booking");
                    }
                    return featureService.getFeatureValueInPlan(booking.planId, featureId);
                };
                return customerService.getFirstActiveBookingForProduct(product).then(bookingHandler);
            };
            utilsInstance.stringEndsWith = function(str, supposedSuffix) {
                return str.slice(-supposedSuffix.length) === supposedSuffix;
            };
            utilsInstance.convertToInt = function(data) {
                if (typeof data !== "number") {
                    data = parseInt(data);
                }
                return data;
            };
            utilsInstance.stringNotEmpty = function(data) {
                return (data != null) && data.length >= 1;
            };
            utilsInstance.getTestRunsLeftDesc = function(value) {
                var checkValue;
                if (value === "blanco") {
                    return "";
                } else {
                    checkValue = this.convertToInt(value);
                    if (isNaN(checkValue)) {
                        console.warn("Unknown accounting value '" + value + "' in getTestRunsLeftDesc-function");
                        return "";
                    }
                    if (checkValue < 0) {
                        return "running Tests was not booked";
                    }
                    if (checkValue === 0) {
                        return "no Test Runs left for today";
                    } else {
                        return checkValue + " Test Run" + (value === 1 ? " " : "s ") + "left for today";
                    }
                }
            };
            utilsInstance.shortenStringAtEnd = function(str, lgnt) {
                if ((str != null)) {
                    if ((lgnt != null) && str.length > lgnt) {
                        str = str.substring(0, lgnt - 3) + "...";
                    } else if (str.length > utilsInstance.defaultMaxStringLength) {
                        str = str.substring(0, utilsInstance.defaultMaxStringLength - 3) + "...";
                    }
                }
                return str;
            };
            utilsInstance.shortenStringInCenter = function(str, lgnt) {
                var divider;
                if ((str != null)) {
                    divider = 0;
                    if ((lgnt != null) && str.length > lgnt) {
                        divider = lgnt / 2;
                    } else if (str.length > utilsInstance.defaultMaxStringLength) {
                        divider = utilsInstance.defaultMaxStringLength / 2;
                    }
                    if (divider !== 0) {
                        str = str.substring(0, divider - 3) + "..." + str.substring(str.length - divider, str.length);
                    }
                    return str;
                }
            };
            utilsInstance.toTimeZoneAwareTimeString = function(timeAsDateFromDB, userOpt) {
                var time;
                if (typeof timeAsDateFromDB === 'number' && (timeAsDateFromDB % 1) === 0) {
                    time = moment(timeAsDateFromDB);
                } else {
                    time = moment(timeAsDateFromDB, moment.ISO_8601);
                }
                if (userOpt && userOpt.timeZoneIdentifier) {
                    time = time.zone(-parseInt(userOpt.timeZoneIdentifier.substring(7)));
                }
                return time.calendar();
            };
            utilsInstance.emptyIfUndefined = function(val) {
                if ((val != null)) {
                    return val;
                }
                return "";
            };
            utilsInstance.removeNonNumericChars = function(str) {
                var c, i, result, val, _i, _ref;
                result = "";
                for (i = _i = 0, _ref = str.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
                    val = str.charAt(i);
                    c = parseInt(val, 10);
                    if (c >= 0 && c <= 9) {
                        result = result + c;
                    }
                }
                return result;
            };
            utilsInstance.copy = function(item) {
                return JSON.parse(JSON.stringify(item));
            };
            utilsInstance.verifyValidURLs = function(listOfURLs) {
                var url, _i, _len;
                for (_i = 0, _len = listOfURLs.length; _i < _len; _i++) {
                    url = listOfURLs[_i];
                    if (utilsInstance.verifyURL(url) !== "valid") {
                        return false;
                    }
                }
                return true;
            };
            utilsInstance.verifyURL = function(url) {
                if (url == null) {
                    return "invalid";
                } else if (url.length === 0) {
                    return "empty";
                } else {
                    return "valid";
                }
            };
            utilsInstance.prepareStringForCCcheck = function(str) {
                var c, i, noNumbers, numbers, val, _i, _ref;
                numbers = "";
                noNumbers = "";
                for (i = _i = 0, _ref = str.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
                    val = str.charAt(i);
                    c = parseInt(val, 10);
                    if (c >= 0 && c <= 9) {
                        numbers = c + numbers;
                    } else {
                        noNumbers = val + noNumbers;
                    }
                }
                return {
                    numbers: numbers,
                    noNumbers: noNumbers
                };
            };
            utilsInstance.removeDuplicates = function(array) {
                var key, res, value, _i, _ref, _results;
                if (array.length === 0) {
                    return [];
                }
                res = {};
                for (key = _i = 0, _ref = array.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; key = 0 <= _ref ? ++_i : --_i) {
                    res[array[key]] = array[key];
                }
                _results = [];
                for (key in res) {
                    value = res[key];
                    _results.push(value);
                }
                return _results;
            };
            utilsInstance.uuidToNumber = function(value) {
                var data, hash, i, number, result, str, _i, _ref;
                data = this.prepareStringForCCcheck(value);
                number = parseInt(data.numbers);
                str = data.noNumbers;
                hash = 0;
                for (i = _i = 0, _ref = str.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
                    hash = ((hash << 5) - hash) + str.charCodeAt(i);
                }
                result = hash + number;
                while (result > 10000 || result < -10000) {
                    result = result / 100;
                }
                return parseInt(result);
            };
            utilsInstance.correctURL = function(url) {
                if (!utilsInstance.stringContains(url, "://")) {
                    return "http://" + url;
                }
                return url;
            };
            utilsInstance.DEBUG_iterateOverObjectHelper = function(obj, data) {
                var property, _i, _len, _results;
                _results = [];
                for (_i = 0, _len = obj.length; _i < _len; _i++) {
                    property = obj[_i];
                    if (obj.hasOwnProperty(property)) {
                        if (typeof obj[property] === "object") {
                            _results.push(utilsInstance.DEBUG_iterateOverObjectHelper(obj[property], data + '.' + property));
                        } else {
                            _results.push(console.log(property + "   " + obj[property]));
                        }
                    } else {
                        _results.push(void 0);
                    }
                }
                return _results;
            };
            utilsInstance.DEBUG_iterateOverObject = function(obj) {
                console.log("------------- OBJECT ITERATION --------------");
                utilsInstance.DEBUG_iterateOverObjectHelper(obj, "");
                return console.log("--------- END OF OBJECT ITERATION -----------");
            };
            utilsInstance.validateCreditCardNumber = function(str) {
                var c, i, n, r, t, _i, _j, _ref, _ref1;
                if (!(str != null)) {
                    return false;
                }
                r = this.prepareStringForCCcheck(str).numbers;
                if (!(r.length >= 12)) {
                    return false;
                }
                t = "";
                for (i = _i = 0, _ref = r.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
                    c = parseInt(r.charAt(i), 10);
                    if (i % 2 !== 0) {
                        c *= 2;
                    }
                    t = t + c;
                }
                n = 0;
                for (i = _j = 0, _ref1 = t.length - 1; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
                    c = parseInt(t.charAt(i), 10);
                    n = n + c;
                }
                return !!(n !== 0 && n % 10 === 0);
            };
            return utilsInstance;
        }
    ]);

})(this);